#include <stdio.h>
#include <ctype.h>
#include <string.h>


/*
 *
 * 2011.5.13	china-avs   rain.
 *
 */

static void url_encode_char(char *b, char c)
{
	char hex_table[] = "0123456789ABCDF";

	b[0] = '%';
	b[1] = hex_table[(c >> 4) & 0x0F];
	printf("%c\n", b[1]);	// 6
	b[2] = hex_table[c & 0x0F];
}

int main()
{
	char b[5] = {0};

	url_encode_char(b, 'a');
	printf("main = %s \n", b);	// %61

	return 0;
}


#if 0
/*
 * intel
 */
0000000000001169 <url_encode_char>:
    1169:	f3 0f 1e fa          	endbr64 
    116d:	55                   	push   %rbp
    116e:	48 89 e5             	mov    %rsp,%rbp
    1171:	48 83 ec 30          	sub    $0x30,%rsp
    1175:	48 89 7d d8          	mov    %rdi,-0x28(%rbp)
    1179:	89 f0                	mov    %esi,%eax
    117b:	88 45 d4             	mov    %al,-0x2c(%rbp)
    117e:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    1185:	00 00 
    1187:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    118b:	31 c0                	xor    %eax,%eax
    118d:	48 b8 30 31 32 33 34 	movabs $0x3736353433323130,%rax
    1194:	35 36 37 
    1197:	48 ba 38 39 41 42 43 	movabs $0x46444342413938,%rdx
    119e:	44 46 00 
    11a1:	48 89 45 e0          	mov    %rax,-0x20(%rbp)
    11a5:	48 89 55 e8          	mov    %rdx,-0x18(%rbp)
    11a9:	48 8b 45 d8          	mov    -0x28(%rbp),%rax
    11ad:	c6 00 25             	movb   $0x25,(%rax)
    11b0:	0f b6 45 d4          	movzbl -0x2c(%rbp),%eax
    11b4:	c0 f8 04             	sar    $0x4,%al
    11b7:	0f be c0             	movsbl %al,%eax
    11ba:	83 e0 0f             	and    $0xf,%eax
    11bd:	89 c1                	mov    %eax,%ecx
    11bf:	48 8b 45 d8          	mov    -0x28(%rbp),%rax
    11c3:	48 8d 50 01          	lea    0x1(%rax),%rdx
    11c7:	48 63 c1             	movslq %ecx,%rax
    11ca:	0f b6 44 05 e0       	movzbl -0x20(%rbp,%rax,1),%eax
    11cf:	88 02                	mov    %al,(%rdx)
    11d1:	48 8b 45 d8          	mov    -0x28(%rbp),%rax
    11d5:	48 83 c0 01          	add    $0x1,%rax
    11d9:	0f b6 00             	movzbl (%rax),%eax
    11dc:	0f be c0             	movsbl %al,%eax
    11df:	89 c6                	mov    %eax,%esi
    11e1:	48 8d 3d 1c 0e 00 00 	lea    0xe1c(%rip),%rdi        # 2004 <_IO_stdin_used+0x4>
    11e8:	b8 00 00 00 00       	mov    $0x0,%eax
    11ed:	e8 7e fe ff ff       	callq  1070 <printf@plt>
    11f2:	0f be 45 d4          	movsbl -0x2c(%rbp),%eax
    11f6:	83 e0 0f             	and    $0xf,%eax
    11f9:	89 c1                	mov    %eax,%ecx
    11fb:	48 8b 45 d8          	mov    -0x28(%rbp),%rax
    11ff:	48 8d 50 02          	lea    0x2(%rax),%rdx
    1203:	48 63 c1             	movslq %ecx,%rax
    1206:	0f b6 44 05 e0       	movzbl -0x20(%rbp,%rax,1),%eax
    120b:	88 02                	mov    %al,(%rdx)
    120d:	90                   	nop
    120e:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1212:	64 48 33 04 25 28 00 	xor    %fs:0x28,%rax
    1219:	00 00 
    121b:	74 05                	je     1222 <url_encode_char+0xb9>
    121d:	e8 3e fe ff ff       	callq  1060 <__stack_chk_fail@plt>
    1222:	c9                   	leaveq 
    1223:	c3                   	retq   

0000000000001224 <main>:
    1224:	f3 0f 1e fa          	endbr64 
    1228:	55                   	push   %rbp
    1229:	48 89 e5             	mov    %rsp,%rbp
    122c:	48 83 ec 10          	sub    $0x10,%rsp
    1230:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    1237:	00 00 
    1239:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    123d:	31 c0                	xor    %eax,%eax
    123f:	c7 45 f3 00 00 00 00 	movl   $0x0,-0xd(%rbp)
    1246:	c6 45 f7 00          	movb   $0x0,-0x9(%rbp)
    124a:	48 8d 45 f3          	lea    -0xd(%rbp),%rax
    124e:	be 61 00 00 00       	mov    $0x61,%esi
    1253:	48 89 c7             	mov    %rax,%rdi
    1256:	e8 0e ff ff ff       	callq  1169 <url_encode_char>
    125b:	48 8d 45 f3          	lea    -0xd(%rbp),%rax
    125f:	48 89 c6             	mov    %rax,%rsi
    1262:	48 8d 3d 9f 0d 00 00 	lea    0xd9f(%rip),%rdi        # 2008 <_IO_stdin_used+0x8>
    1269:	b8 00 00 00 00       	mov    $0x0,%eax
    126e:	e8 fd fd ff ff       	callq  1070 <printf@plt>
    1273:	b8 00 00 00 00       	mov    $0x0,%eax
    1278:	48 8b 55 f8          	mov    -0x8(%rbp),%rdx
    127c:	64 48 33 14 25 28 00 	xor    %fs:0x28,%rdx
    1283:	00 00 
    1285:	74 05                	je     128c <main+0x68>
    1287:	e8 d4 fd ff ff       	callq  1060 <__stack_chk_fail@plt>
    128c:	c9                   	leaveq 
    128d:	c3                   	retq   
    128e:	66 90                	xchg   %ax,%ax

/*
 * arm v8
 */
static void url_encode_char(char *b, char c)
{
  400590:	a9bd7bfd 	stp	x29, x30, [sp, #-48]!
  400594:	910003fd 	mov	x29, sp
  400598:	f9000fa0 	str	x0, [x29, #24] ; *b
  40059c:	39005fa1 	strb	w1, [x29, #23] ; c
	char hex_table[] = "0123456789ABCDF";
  4005a0:	90000000 	adrp	x0, 400000 <_init-0x3c8>
  4005a4:	911c2000 	add	x0, x0, #0x708 ; parameter
  4005a8:	a9400400 	ldp	x0, x1, [x0]   ; x0, x1 <--- [x0]
  4005ac:	a90207a0 	stp	x0, x1, [x29, #32] ; x0, x1 ---> x29 + 32   hex_table

	b[0] = '%';
  4005b0:	f9400fa0 	ldr	x0, [x29, #24] ; *b
  4005b4:	528004a1 	mov	w1, #0x25                  	// #37 
  4005b8:	39000001 	strb	w1, [x0]                ; b[0] = '%'
	b[1] = hex_table[(c >> 4) & 0x0F];
  4005bc:	f9400fa0 	ldr	x0, [x29, #24] ; x0 = *b
  4005c0:	91000400 	add	x0, x0, #0x1   ; x0 = *b + 1 = b[1]
  4005c4:	39405fa1 	ldrb	w1, [x29, #23] ; w1 = c
  4005c8:	53047c21 	lsr	w1, w1, #4         ; w1 = c >> 4
  4005cc:	53001c21 	uxtb	w1, w1         ; w1 = extend unsigned
  4005d0:	12000c21 	and	w1, w1, #0xf       ; w1 = (c>>4)&0xf
  4005d4:	93407c21 	sxtw	x1, w1         ; x1 = extend 64bit (w1)
  4005d8:	910083a2 	add	x2, x29, #0x20     ; x2 = hex_table
  4005dc:	38616841 	ldrb	w1, [x2, x1]   ; w1 = hex_table + (c>>4)&0xf
  4005e0:	39000001 	strb	w1, [x0]       ; x0 = w1 ---> b[1] = hex_table + (c>>4)&0xf
	printf("%c\n", b[1]);
  4005e4:	f9400fa0 	ldr	x0, [x29, #24] ; *b
  4005e8:	91000400 	add	x0, x0, #0x1   ; x0 = x0+1 = b[1]
  4005ec:	39400000 	ldrb	w0, [x0]   ; w0 = b[1]
  4005f0:	2a0003e1 	mov	w1, w0         ; w1 = b[1]
  4005f4:	90000000 	adrp	x0, 400000 <_init-0x3c8>
  4005f8:	911c0000 	add	x0, x0, #0x700 ; parameter
  4005fc:	97ffff8d 	bl	400430 <printf@plt>
	b[2] = hex_table[c & 0x0F];
  400600:	f9400fa0 	ldr	x0, [x29, #24] ; *b
  400604:	91000800 	add	x0, x0, #0x2   ; x0 = b+2 = b[2]
  400608:	39405fa1 	ldrb	w1, [x29, #23] ; w1 = c
  40060c:	12000c21 	and	w1, w1, #0xf       ; w1 = c & 0xf
  400610:	93407c21 	sxtw	x1, w1         ; x1 = extend 64bit (c & 0xf)
  400614:	910083a2 	add	x2, x29, #0x20     ; x2 = x29 + 32, hex_table
  400618:	38616841 	ldrb	w1, [x2, x1]   ; w1 = hex_table + (c & 0xf)
  40061c:	39000001 	strb	w1, [x0]       ; b[2] = hex_table[c & 0xf]
}
  400620:	d503201f 	nop
  400624:	a8c37bfd 	ldp	x29, x30, [sp], #48 ; x29, x30 <--- sp, sp+48
  400628:	d65f03c0 	ret

000000000040062c <main>:

int main()
{
  40062c:	a9be7bfd 	stp	x29, x30, [sp, #-32]! ; x29, x30 ---> sp-32, sp=sp-32 
  400630:	910003fd 	mov	x29, sp
	char b[5] = {0};
  400634:	b9001bbf 	str	wzr, [x29, #24]     ; x29+24 = 0 ----> b
  400638:	390073bf 	strb	wzr, [x29, #28] ; x29+28 = 0 ---->  24, 25, 26, 27, 28

	url_encode_char(b, 'a');
  40063c:	910063a0 	add	x0, x29, #0x18 ; x0 = b
  400640:	52800c21 	mov	w1, #0x61                  	// #97
  400644:	97ffffd3 	bl	400590 <url_encode_char>
	printf("main = %s \n", b);
  400648:	910063a1 	add	x1, x29, #0x18 ; b
  40064c:	90000000 	adrp	x0, 400000 <_init-0x3c8>
  400650:	911c6000 	add	x0, x0, #0x718 ; parameter
  400654:	97ffff77 	bl	400430 <printf@plt>

	return 0;
  400658:	52800000 	mov	w0, #0x0                   	// #0
}

/*
 * arm v7
 */
static void url_encode_char(char *b, char c)
{
   10490:	e92d4800 	push	{fp, lr}
   10494:	e28db004 	add	fp, sp, #4
   10498:	e24dd018 	sub	sp, sp, #24
   1049c:	e50b0018 	str	r0, [fp, #-24]	; 0xffffffe8        *b
   104a0:	e1a03001 	mov	r3, r1
   104a4:	e54b3019 	strb	r3, [fp, #-25]	; 0xffffffe7    c
	char hex_table[] = "0123456789ABCDF";
   104a8:	e59f307c 	ldr	r3, [pc, #124]	; 1052c <url_encode_char+0x9c>  parameter
   104ac:	e24bc014 	sub	ip, fp, #20     ; ip = fp - 20  ---> hex_table
   104b0:	e893000f 	ldm	r3, {r0, r1, r2, r3} ; r0=r3, r1=r3+4, r2=r3+8, r3=r3+12
   104b4:	e88c000f 	stm	ip, {r0, r1, r2, r3} ; ip=r0, ip+4=r1, ip+8=r2, ip+12=r3

	b[0] = '%';
   104b8:	e51b3018 	ldr	r3, [fp, #-24]	; 0xffffffe8    *b
   104bc:	e3a02025 	mov	r2, #37	; 0x25
   104c0:	e5c32000 	strb	r2, [r3]   ; b[0] = %
	b[1] = hex_table[(c >> 4) & 0x0F];
   104c4:	e51b3018 	ldr	r3, [fp, #-24]	; 0xffffffe8    *b
   104c8:	e2833001 	add	r3, r3, #1      ; r3 = *b+1
   104cc:	e55b2019 	ldrb	r2, [fp, #-25]	; 0xffffffe7   r2 =  c
   104d0:	e1a02222 	lsr	r2, r2, #4          ; r2 = c>>4
   104d4:	e20220ff 	and	r2, r2, #255	; 0xff r2 = c>>4 & 0xff
   104d8:	e202200f 	and	r2, r2, #15     ; r2 = (c>>4) & 0xff & 0xf
   104dc:	e24b1004 	sub	r1, fp, #4      ; r1 = fp - 4
   104e0:	e0812002 	add	r2, r1, r2      ; r2 = (fp -4) + ((c>>4)&0xff&0xf)
   104e4:	e5522010 	ldrb	r2, [r2, #-16] ; r2 = ((fp-4)+((c>>r)&0xff&0xf)-20), hex_table[(c>>4)&0xf] 
   104e8:	e5c32000 	strb	r2, [r3]       ; b[1] = r2
	printf("%c\n", b[1]);
   104ec:	e51b3018 	ldr	r3, [fp, #-24]	; 0xffffffe8    *b
   104f0:	e2833001 	add	r3, r3, #1      ; *b + 1
   104f4:	e5d33000 	ldrb	r3, [r3]    ; r3 = b[1]
   104f8:	e1a01003 	mov	r1, r3          ; r1 = b[1]
   104fc:	e59f002c 	ldr	r0, [pc, #44]	; 10530 <url_encode_char+0xa0>
   10500:	ebffff82 	bl	10310 <printf@plt>
	b[2] = hex_table[c & 0x0F];
   10504:	e51b3018 	ldr	r3, [fp, #-24]	; 0xffffffe8    *b
   10508:	e2833002 	add	r3, r3, #2      ; *b+2
   1050c:	e55b2019 	ldrb	r2, [fp, #-25]	; 0xffffffe7    c
   10510:	e202200f 	and	r2, r2, #15         ; r2 = c&0xf
   10514:	e24b1004 	sub	r1, fp, #4          ; r1 = fp-4
   10518:	e0812002 	add	r2, r1, r2          ; r2 = (fp-4)+c&0xf
   1051c:	e5522010 	ldrb	r2, [r2, #-16]  ; r2 = (fp-4)+c&0xf-16, fp-20 + c&0xf, hex_table[c&0xf]
   10520:	e5c32000 	strb	r2, [r3]        ; b[1] = r2
}
   10524:	e24bd004 	sub	sp, fp, #4
   10528:	e8bd8800 	pop	{fp, pc}
   1052c:	00010598 	.word	0x00010598
   10530:	00010594 	.word	0x00010594

00010534 <main>:

int main()
{
   10534:	e92d4800 	push	{fp, lr}
   10538:	e28db004 	add	fp, sp, #4
   1053c:	e24dd008 	sub	sp, sp, #8
	char b[5] = {0};
   10540:	e3a03000 	mov	r3, #0
   10544:	e50b300c 	str	r3, [fp, #-12] ; 12 11 10 9
   10548:	e3a03000 	mov	r3, #0
   1054c:	e54b3008 	strb	r3, [fp, #-8] ; 8

	url_encode_char(b, 'a');
   10550:	e24b300c 	sub	r3, fp, #12 ; b
   10554:	e3a01061 	mov	r1, #97	; 0x61
   10558:	e1a00003 	mov	r0, r3
   1055c:	ebffffcb 	bl	10490 <url_encode_char>
	printf("main = %s \n", b);
   10560:	e24b300c 	sub	r3, fp, #12 ; b
   10564:	e1a01003 	mov	r1, r3
   10568:	e59f0010 	ldr	r0, [pc, #16]	; 10580 <main+0x4c>
   1056c:	ebffff67 	bl	10310 <printf@plt>

	return 0;
   10570:	e3a03000 	mov	r3, #0
}

#endif

